package com.jbpm.websercvice.manager.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.emms.common.util.BaseUtil;
import com.emms.common.util.BeanUtilsImprove;
import com.jbpm.websercvice.manager.IServerManager;
import com.jbpm.websercvice.manager.VCurrencyService;
import com.jbpm.websercvice.vo.WfParamInfoVo;
import com.jbpm.websercvice.vo.WfTaskJobBean;
import com.jbpm.wf.service.WfDrawImageManager;
import com.jbpm.wf.service.WfProcessManager;
import com.jbpm.wf.service.WfTaskManager;


/**
 * @功能:通过cxf实现webservice--为其他系统提供工作流服务
 * @author win
 *
 */
//注解指明了如何将Web服务绑定到SOAP消息上(进行暴露处理)
@SOAPBinding(style =Style.RPC)
//注解是将一个普通类定义为Web服务发布对象（亦称终端，endpoint）
@WebService(endpointInterface = "com.jbpm.websercvice.manager.IServerManager", serviceName = "ServerManager")
public class ServerManagerImpl implements IServerManager{

	@SuppressWarnings("unused")
	private Log logger=LogFactory.getLog(ServerManagerImpl.class);

	//通用service调用
	@Resource(name = "vCurrencyServiceImpl")
	private VCurrencyService vCurrencyService;
	//流程处理
	@Resource(name="wfProcessManager")
	private WfProcessManager wfProcessManager;
	//流程图处理
	@Resource(name="wfDrawImageManager")
	private WfDrawImageManager wfDrawImageManager;
	
	//任务处理
	@Resource(name="wfTaskManager")
	private WfTaskManager wfTaskManager;
	
	@Resource(name="hibernateTemplate")
	private HibernateTemplate hibernateTemplate;
	
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	
	/**
	 * 流程图进度获取
	 * @param pram
	 * @return
	 */
	//@Transactional(readOnly=true)
	public WfParamInfoVo clientTaskImageByParam(WfParamInfoVo pram) {
		try {
			//获取首节点————处理人员
			pram=wfTaskManager.clientTaskImageByParam(pram);
			//获取流程图
			if(1==pram.getRsult()){
				pram=wfDrawImageManager.getTaskImageByParam(pram);
			}
		} catch (Exception e) {
			e.printStackTrace();
			pram.setRsult(2);
			pram.setErrorRsult("服务调用异常!请联系管理员!");
		}
		//返回客户端使用
		WfParamInfoVo newParam=new WfParamInfoVo();
		if(!BaseUtil.isEmpty(pram)){
			BeanUtilsImprove.copyProperties(pram, newParam);
			pram=null;
			
		}
		seesionFunction();
		return newParam;
	}

	/**
	 * 得到当前登陆人的所有活动节点
	 * @param param
	 * @return
	 */
	//@Transactional(readOnly=true)
	public List<WfTaskJobBean> clientUserTaskJob(WfTaskJobBean param){
		
		List<WfTaskJobBean> resList=null;
		try {
			resList=wfTaskManager.clientUserTaskJob(param);
		} catch (Exception e) {
			e.printStackTrace();  
			resList=new ArrayList<WfTaskJobBean>();
		}
		//返回客户端使用
		if(!BaseUtil.isEmpty(param)){
			param=null;
			
		}
		seesionFunction();
		return resList;
	}
	
	/**
     * 流程提交
     * @param wfParam
     * @return
     */
	//@Transactional(rollbackFor=Exception.class)
	public WfParamInfoVo submitAudit(WfParamInfoVo wfParam) {
		try {
			wfParam=wfTaskManager.clientSubmitAudit(wfParam);
		} catch (Exception e) {
			e.printStackTrace();
			wfParam.setRsult(2);
			wfParam.setErrorRsult("服务调用异常!请联系管理员!");
		}
		//返回客户端使用
		WfParamInfoVo newParam=new WfParamInfoVo();
		if(!BaseUtil.isEmpty(wfParam)){
			BeanUtilsImprove.copyProperties(wfParam, newParam);
			wfParam=null;
			
		}
		seesionFunction();
		return newParam;
	}
	

	/**
	 * 获取相应流程信息
	 * @param pram
	 * @return
	 */
	//@Transactional(readOnly=true)
	public WfParamInfoVo getWfByParam(WfParamInfoVo pram) {
		try {
			//获取首节点————处理人员
			pram=wfProcessManager.getWfByParam(pram);
			//获取流程图
			if(1==pram.getRsult()){
				pram=wfDrawImageManager.getTaskImageByParam(pram);
			}
		} catch (Exception e) {
			e.printStackTrace();
			pram.setRsult(2);
			pram.setErrorRsult("服务调用异常!请联系管理员!");
		}
		//返回客户端使用
		WfParamInfoVo newParam=new WfParamInfoVo();
		if(!BaseUtil.isEmpty(pram)){
			BeanUtilsImprove.copyProperties(pram, newParam);
			pram=null;
			
		}
		seesionFunction();
		return newParam;
	}
	

	//-----------------test--------------------
	private void seesionFunction(){
		/*try {
			Session s=sessionFactory.getCurrentSession();
			Session so=sessionFactory.openSession();
			s.flush();
			//s.clear();
			so.flush();
			//so.clear();
			//sessionFactory.getReference();
			
			// MBean service registration for a specific SessionFactory
			Hashtable tb = new Hashtable();
			tb.put("type", "statistics");
			tb.put("sessionFactory", "myFinancialApp");
			ObjectName on = new ObjectName("hibernate", tb); // MBean object name
	
			StatisticsService stats = new StatisticsService(); // MBean implementation
			stats.setSessionFactory(sessionFactory); // Bind the stats to a SessionFactory
			//server.registerMBean(stats, on); // Register the Mbean on the server
			
			sessionFactory.getStatistics().setStatisticsEnabled(true);
			
			 // 创建Statistics对象，并通过SessionFactory对象获得统计信息
			  Statistics st = sessionFactory.getStatistics();
			  // 打印全部统计信息
			  System.out.println("========Statistics 统计信息==========start=======================");
			  System.out.println("-打印全部统计信息--"+st);
			  System.out.println("-getSessionOpenCount--"+st.getSessionOpenCount());
			  System.out.println("-getSessionCloseCount--"+st.getSessionCloseCount());
			  // 打印二级缓存信息
			  System.out.println("-打印二级缓存信息--"+st.getSecondLevelCacheHitCount());
			  System.out.println("==================end=======================");
			  
			
			// MBean service registration for all SessionFactory's
			Hashtable tb = new Hashtable();
			tb.put("type", "statistics");
			tb.put("sessionFactory", "all");
			ObjectName on = new ObjectName("hibernate", tb); // MBean object name
	
			StatisticsService stats = new StatisticsService(); // MBean implementation
			server.registerMBean(stats, on); // Register the MBean on the server
			
		} catch (Exception e) {
			e.printStackTrace();
		}*/
	   }
   
}
